package com.antler.smt.admin.service.impl;

import com.antler.smt.admin.mapper.OrderInfoMapper;
import com.antler.smt.admin.mapper.RequirementEntityMapper;
import com.antler.smt.admin.mapper.RequirementInfoMapper;
import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.constant.OrderConstant;
import com.antler.smt.entity.OrderEntity;
import com.antler.smt.entity.RequirementEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.request.admin.EditRequirementAddressReq;
import com.antler.smt.request.admin.GetRequirementListReq;
import com.antler.smt.response.admin.GetRequirementListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.admin.RequirementService;
import com.antler.smt.utils.DateUtil;
import com.antler.smt.utils.LogUtil;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *  获取用户发布需求列表
 *
 * @author zhangyi
 * @date 2018/3/23 15:23
 */
@Repository
public class RequirementServiceImpl implements RequirementService {

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Autowired
    private RequirementEntityMapper requirementEntityMapper;

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 获取发布信息实体
     *
     * @param requirementId 请求实体
     * @return
     */
    @Override
    public Result<GetRequirementListResp> requirementInfo(long requirementId) {
        Result result = null;

        RequirementEntity requirementEntity = requirementEntityMapper.selectByPrimaryKey(requirementId);

        result = new Result(requirementEntity);

        return result;
    }

    /**
     *  需求驳回
     *
     * @param requirementId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result<ResultCodeEnum> requirementFail(Long requirementId) {
        Result result = null;

        //创建订单实体
        OrderEntity orderEntity = new OrderEntity();

        //添加需求id
        orderEntity.setRequirementId(requirementId);

        //驳回时间
        orderEntity.setRejectTime(DateUtil.stampToDate(System.currentTimeMillis()));

        //驳回标志 100
        orderEntity.setStatus(OrderConstant.ORDER_FAIL);

        //更新时间
        orderEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));

        int i = orderInfoMapper.updateOrderRejectStatus(orderEntity);

        if(0 == i){
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
        }else{
            result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
        }
        return result;
    }

    /**
     * 需求修改
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result<ResultCodeEnum> requirementUpdate(RequirementEntity request) {
        Result result = null;

        int i = requirementEntityMapper.updateByPrimaryKeySelective(request);

        if(0 == i){
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
        }else{
            result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
        }
        return result;
    }

    /**
     *  需求通过
     *
     * @param requirementId
     * @return
     */
    @Override
    public Result<ResultCodeEnum> requirementPass(Long requirementId) {
        Result result = null;

        //创建订单实体
        OrderEntity orderEntity = new OrderEntity();
        //更新订单状态(待对接服务商)
        orderEntity.setStatus(OrderConstant.ORDER_WAIT_JOIN_SUPPLIER);
        //更新最新时间
        orderEntity.setUpdateTime(DateUtil.stampToDate(System.currentTimeMillis()));
        //更新需求id
        orderEntity.setRequirementId(requirementId);
        int i = orderInfoMapper.requirementPass(orderEntity);
        if(0 == i){
            result = new Result(ResultCodeEnum.APPEND_DATA_ERROR);
        }else{
            result = new Result(ResultCodeEnum.OPERATION_SUCCESS);
        }
        return result;
    }

    /**
     * 修改发布需求的地址
     *
     * @param req
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result<ResultCodeEnum> editRequirementAddress(EditRequirementAddressReq req) {
        Result result = null;

        RequirementEntity requirementEntity = new RequirementEntity();
        requirementEntity.setId(req.getRequirementId());
        requirementEntity.setConsigneeName(req.getConsigneeName());
        requirementEntity.setConsigneePhone(req.getConsigneePhone());
        requirementEntity.setConsigneeAdress(req.getConsigneeAdress());
        int i = requirementEntityMapper.updateByPrimaryKeySelective(requirementEntity);
        result = (0 == i? new Result(ResultCodeEnum.APPEND_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));
        return result;
    }
}
